//
//  WBBaseController.swift
//  weibo
//
//  Created by 李江波 on 16/10/6.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit

class WBBaseController: UIViewController {

    //是否登录
    var visitor = false
    
    var visitorBaceInfo : [String : String]?
    
    
    
    /// 自定义导航条
    lazy var navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
    
    lazy var navItem = UINavigationItem()
    
    var tableView : UITableView?

    var refreshControl : UIRefreshControl?
    //上拉刷新标记
    var isPullup = false
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        loadData()
    }
    
    func loadData() {
        
        refreshControl?.endRefreshing()
    }
    
    /// 重写title 的set方法
    override var title: String?{
    
        didSet{
            
            navItem.title = title
            
        }
    }
    
    
   
}

// MARK: - 登录，注册按钮时间
extension WBBaseController {

    @objc fileprivate func registerClick(){
    
        print("注册按钮")
    
    }
    
    @objc fileprivate func loginClick() {
        
        print("登录按钮")
    }


}


// MARK: - 搭建UI
extension WBBaseController {
    
    
    fileprivate func setupUI()  {
        
        //取消自动缩进，如果隐藏了导航栏。会缩进20 个点
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavBar()
       
        visitor ? setupTableView() : setupVisitorView()
        
    }
    
    
    /// 设置访客视图
    private func setupVisitorView() {
    
        let visitorView = WBVisitorView(frame: view.bounds)
        
        
        view.insertSubview(visitorView, belowSubview: navBar)
      
        visitorView.visitorInfo = visitorBaceInfo
        
        visitorView.registerButton.addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        
        visitorView.loginButton.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerClick))
        
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginClick))
    }
    
   /// 设置导航条
    private func setupNavBar() -> () {
        
        view.backgroundColor = UIColor(alpha: 1)
        
        view.addSubview(navBar)
        
        navBar.items = [navItem]
        
        navBar.tintColor = UIColor(rgbValue: 0xf6f600)
        
        navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.lightGray]
    
        navBar.tintColor = UIColor.orange
    }
    
    func setupTableView() {
    
    tableView = UITableView(frame: view.bounds)
    
    view.insertSubview(tableView!, belowSubview: navBar)
    
    tableView?.delegate = self;
    tableView?.dataSource = self;
    
    tableView?.contentInset = UIEdgeInsets(top: navBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
    
    //向下刷新菊花
    refreshControl = UIRefreshControl()
    
    tableView?.addSubview(refreshControl!)
    
    refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    
    
    
    }
}


// MARK: - UITableViewDelegate,UITableViewDataSource
extension WBBaseController:UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
   
    //往上拉刷洗
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let section = tableView.numberOfSections - 1
        let count = tableView.numberOfRows(inSection: section)
        
        if indexPath.row == count - 1  {
            
            isPullup = true
            loadData()
        }
        
    }
    
}





